//
//  HomeTableViewController.swift
//  ZMWB
//
//  Created by 朱敏 on 2017/9/19.
//  Copyright © 2017年 朱敏. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.判断用户是否登录
        if !isLogin {
            // 设置访客视图
            visitorView?.setupVisitorInfo(imageName: nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        // 2.初始化导航条
        setupNav()
        
        // 3.注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(titleChange), name: NSNotification.Name(ZMPresentationManagerDidPresented), object: animatorManager)
        NotificationCenter.default.addObserver(self, selector: #selector(titleChange), name: NSNotification.Name(ZMPresentationManagerDidDismissed), object: animatorManager)
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - 内部控制方法
    
    private func setupNav() {
        // 1.添加左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendattention", target: self, action: #selector(leftBtnClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(rightBtnClick))
        
        // 2.添加标题按钮
        navigationItem.titleView = titleButton
    }
    
    @objc private func titleChange() {
        titleButton.isSelected = !titleButton.isSelected
    }
    
    @objc private func titleBtnClick(btn: TitleButton) {
        
        // 2.显示菜单
        // 2.1创建菜单
        let menuVC = PopoverViewController()
        
        // 自定义转场动画
        // 设置转场代理
        // 设置转场动画样式
        menuVC.transitioningDelegate = animatorManager
        menuVC.modalPresentationStyle = UIModalPresentationStyle.custom
        // 2.2弹出菜单
        present(menuVC, animated: true, completion: nil)
    }
    
    @objc private func leftBtnClick() {
        ZMLog(message: "1")
    }
    
    @objc private func rightBtnClick() {
        // 1.创建二维码控制器
        let vc = QRCodeViewController()
        present(vc, animated: true, completion: nil)
        
        // 2.弹出二维码控制器
    }
    
    // MARK: - 懒加载
    private lazy var animatorManager: ZMPresentationManager = {
        let manager = ZMPresentationManager()
        manager.presentFrame = CGRect(x: 100, y: 45, width: 200, height: 200)
        return manager
    }()
    
    // 标题按钮
    private lazy var titleButton: TitleButton = {
        let btn = TitleButton()
        btn.setTitle("九哥", for: UIControlState.normal)
        btn.addTarget(self, action: #selector(titleBtnClick(btn:)), for: UIControlEvents.touchUpInside)
        return btn
    }()
}

